<?php

	// TITLE: Address AUTO MODEL
	// FILE: scaffolding/model/address.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createAddress = function($con, $address) {
		$sql = 'INSERT INTO address (id, street_1, street_2, street_3, city, state_id, zip_code) VALUES (';
		$sql .= $address->getId() . ', ';
		$sql .= $address->getStreet1() . ', ';
		$sql .= $address->getStreet2() . ', ';
		$sql .= $address->getStreet3() . ', ';
		$sql .= $address->getCity() . ', ';
		$sql .= $address->getStateId() . ', ';
		$sql .= $address->getZipCode() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readAddress = function($con, $id) {
		$sql = 'SELECT * FROM address WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$address = new Address();
			$address->setId(result[0]);
			$address->setStreet1(result[1]);
			$address->setStreet2(result[2]);
			$address->setStreet3(result[3]);
			$address->setCity(result[4]);
			$address->setStateId($dbio->select($readState(result[5])));
			$address->setZipCode(result[6]);
		} else {
			$address = false;
		}
		return $address;
	};

	$updateAddress = function($con, $address) {
		$sql = 'UPDATE address SET ';
		$sql .= 'id = ' . $address->getId() . ', ';
		$sql .= 'street_1 = ' . $address->getStreet1() . ', ';
		$sql .= 'street_2 = ' . $address->getStreet2() . ', ';
		$sql .= 'street_3 = ' . $address->getStreet3() . ', ';
		$sql .= 'city = ' . $address->getCity() . ', ';
		$sql .= 'state_id = ' . $address->getStateId() . ', ';
		$sql .= 'zip_code = ' . $address->getZipCode() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteAddress = function($con, $id) {
		$sql = 'DELETE FROM address WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listAddress = function($con) {
		$sql = 'SELECT * FROM address';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$addresss = array();
			while ($result = mysqli_fetch_array($results)) {
				$address = new Address();
				$address->setId(result[0]);
				$address->setStreet1(result[1]);
				$address->setStreet2(result[2]);
				$address->setStreet3(result[3]);
				$address->setCity(result[4]);
				$address->setStateId($dbio->select($readState(result[5])));
				$address->setZipCode(result[6]);
				$addresss[] = address;
			}// end while
		} else {
			$addresss = false;
		}
		return $addresss;
	};

